<?php

namespace app\merchant\controller\market\card;

use app\merchant\controller\Base;
use app\merchant\model\card\Card as CardModel;
use app\merchant\model\Order as OrderModel;
/**
 * 秒杀商品管理
 * Class Goods
 * @package app\merchant\controller\apps\sharp
 */
class Card extends Base
{
    /**
     * 储值卡列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        $model = new CardModel;
        $list = $model->getList($param);

        return $this->renderSuccess($list,'查询成功');
    }


    /**
     * 储值卡详情
     * @param string $card_id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['card_id']) || isZeroNumeric($param['card_id'])){
            return $this->renderError([],"参数错误");
        }
        // 储值卡详情
        $model = CardModel::detail($param['card_id']);
        if(!$model){
            return $this->renderError([],"储值卡不存在，请刷新重试");
        }
        return $this->renderSuccess($model,'查询成功');
    }

    /**
     * 添加储值卡
     * @param int $step
     * @param null $goods_id
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new CardModel;
        if ($model->add($this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }


    /**
     * 编辑
     * @param $card_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function edit()
    {
        $param = $this->postData();
        if(!isset($param['card_id']) || isZeroNumeric($param['card_id'])){
            return $this->renderError([],"参数错误");
        }
        // 储值卡详情
        $model = CardModel::detail($param['card_id']);
        if(!$model){
            return $this->renderError([],"储值卡不存在，请刷新重试");
        }
        // 更新记录
        if ($model->edit($param)) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '编辑失败');
    }

    /**
     * 删除储值卡
     * @param $card_id
     * @return array
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['card_id']) || isZeroNumeric($param['card_id'])){
            return $this->renderError([],"参数错误");
        }
        // 储值卡详情
        $model = CardModel::detail($param['card_id']);
        if(!$model){
            return $this->renderError([],"储值卡不存在，请刷新重试");
        }
        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }


    /**
     * 储值卡订单列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order()
    {
        // 订单列表
        $model = new OrderModel;
        $list = $model->cardOrder($this->request->param());

        return $this->renderSuccess($list, "查询成功");
    }

    /**
     * 订单导出
     * @param string $dataType
     * @throws \think\exception\DbException
     */
    public function export()
    {
        $model = new OrderModel;

        if (!$model->exportCardList($this->request->param())) {
            return $this->renderError([],$model->getError() ?: '操作失败');
        }

        return $model->exportCardList($this->request->param());
    }


    /**
     * 储值卡订单列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderDel()
    {

        $param = $this->postData();
        if(!isset($param['order_id']) || isZeroNumeric($param['order_id'])){
            return $this->renderError([],"参数错误");
        }

        $model = OrderModel::detail($param['order_id']);

        if(!$model){
            return $this->renderError([],'订单不存在,请刷新后重试');
        }
        if($model['pay_status']['value']==20){
            return $this->renderError([],'已付款订单禁止删除');
        }

        if ($model->orderDel()) {
            return $this->renderSuccess([],'删除成功');
        }
        return $this->renderError([],$model->getError() ?: '删除失败');
    }


}